package com.deng.springboot.demo.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.deng.springboot.demo.facade.entity.User;
import com.deng.springboot.demo.web.model.dto.PageDto;
import com.deng.springboot.demo.web.model.dto.UserAddDto;
import com.deng.springboot.demo.web.model.vo.ResponseResult;
import com.deng.springboot.demo.web.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 邓力宾
 * @date 2020/3/1110:44
 * @Description:
 */
@RestController
@RequestMapping("/user")
@Api(tags = {"用户接口"})
public class UserController {
    private IUserService userService;
    @Autowired
    public void setUserService(IUserService userService){
        this.userService = userService;
    }

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    public ResponseResult<IPage<User>> getPage(@RequestBody PageDto pageDto){
        IPage<User> page = userService.getPage(pageDto.getPageNum(), pageDto.getPageSize());
        return new ResponseResult<>(200, "success", page);
    }

    @ApiOperation(value = "添加用户")
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public ResponseResult<?> addUser(@RequestBody UserAddDto userDto){
        User user = new User();
        if(StringUtils.isEmpty(userDto.getName())){
            return new ResponseResult<>(400, "用户名称不能为空", null);
        }
        user.setName(userDto.getName());
        userService.addUser(user);
        return new ResponseResult<>(200, "success", null);
    }
}
